package sch.guo.crmsys.controller.cop;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.BeanUtils;
import org.springframework.security.core.Authentication;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.beans.factory.annotation.Autowired;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import sch.guo.crmsys.base.ResultUtil;
import sch.guo.crmsys.base.dto.ResultDTO;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

import sch.guo.crmsys.dto.cop.ChartDTO;
import sch.guo.crmsys.dto.cop.InsertOrderDTO;
import sch.guo.crmsys.dto.cop.OrderDTO;
import sch.guo.crmsys.dto.cop.OrderStatementDTO;
import sch.guo.crmsys.entity.auth.AuthUser;
import sch.guo.crmsys.entity.cop.CopOrderDetail;
import sch.guo.crmsys.enums.OrderEnum;
import sch.guo.crmsys.service.auth.AuthUserService;
import sch.guo.crmsys.service.cop.CopOrderDetailService;
import sch.guo.crmsys.service.cop.CopOrderService;
import sch.guo.crmsys.entity.cop.CopOrder;

/**
 * @since 2019-12-31
 */
@RestController
@RequestMapping("/copOrder")
public class CopOrderController {

    @Autowired
    private CopOrderService copOrderService;
    @Autowired
    private CopOrderDetailService copOrderDetailService;
    @Autowired
    private AuthUserService authUserService;

    @PostMapping
    @ApiOperation(value = "插入")
    public ResultDTO insertOne(Authentication authentication,
                               @RequestBody InsertOrderDTO insertOrderDTO) throws Exception {
        CopOrder copOrder = new CopOrder();
        BeanUtils.copyProperties(insertOrderDTO,copOrder);
        AuthUser userDetails = authUserService.getUserDetails(authentication);
        copOrder.setOrderStatus(OrderEnum.START.getCode());
        copOrder.setOrderUserId(userDetails.getUserId());
        copOrderService.save(copOrder);

        CopOrderDetail copOrderDetail = new CopOrderDetail();
        BeanUtils.copyProperties(insertOrderDTO,copOrderDetail);
        copOrderDetail.setOrderId(copOrder.getOrderId());
        copOrderDetailService.save(copOrderDetail);

        return ResultUtil.Success(copOrder.getOrderId());
    }

    @PutMapping
    @ApiOperation(value = "按ID修改")
    public ResultDTO updateById(@RequestBody CopOrder copOrder) throws Exception {
        copOrderService.updateById(copOrder);
        return ResultUtil.Success();
    }

    @DeleteMapping("/{id:\\d+}")
    @ApiOperation("按ID删除")
    public ResultDTO deleteById(@PathVariable Long id) throws Exception {
        copOrderService.removeById((long) id);
        return ResultUtil.Success();
    }

    @GetMapping()
    @ApiOperation("查询、分页返回")
    public ResultDTO selectEntityPage(Authentication authentication,
                                            @RequestParam(value = "page", defaultValue = "0") int pageNum,
                                            @RequestParam(value = "limit", defaultValue = "8") int pageSize,
                                      String clientName)throws Exception{
        AuthUser userDetails = authUserService.getUserDetails(authentication);
        List<OrderDTO> data = copOrderService.selectOrderList(userDetails.getUserId(),clientName, pageNum-1, pageSize);
        for (OrderDTO datum : data) {
            datum.statusConverse();
        }
        int count = copOrderService.countOrderList(userDetails.getUserId(),clientName);
        return ResultUtil.Success(data,count);
    }

    @GetMapping("/{id:\\d+}")
    @ApiOperation("ID查询")
    public ResultDTO selectEntityPage(@PathVariable Long id)throws Exception{
        return ResultUtil.Success(copOrderService.getById(id));
    }

    @PostMapping("/statement")
    @ApiOperation("报表查询")
    public ResultDTO selectStatement(Authentication authentication,
                                      @RequestBody OrderStatementDTO orderStatementDTO)throws Exception{
        List<Long> userId = authUserService.getUserAndChildId(authentication).stream().map(AuthUser::getUserId).collect(Collectors.toList());
        orderStatementDTO.setUserId(userId);
        List<OrderDTO> orderDTOS;
        if (orderStatementDTO.getOrderStatus().size() >1) {
            orderDTOS = copOrderService.selectRunningStatement(orderStatementDTO);
        }else {
            orderDTOS = copOrderService.selectStatement(orderStatementDTO);
        }
        for (OrderDTO orderDTO : orderDTOS) {
            orderDTO.statusConverse();
        }
        return ResultUtil.Success(orderDTOS);
    }

    @PostMapping("/SaleProductSize/{status}")
    @ApiOperation("最近30天售出查询")
    public ResultDTO countSaleProductSize(@PathVariable Integer status) {
        if(status != null && status.equals(1)){
            status = OrderEnum.SUCCESS.getCode();
        }else if(status.equals(0)){
            status = OrderEnum.FAIL.getCode();
        }
        return ResultUtil.Success(copOrderService.countSaleProductSize(status,LocalDateTime.now().minusDays(30L)));
    }

    @PostMapping("/SalePrice/{status}")
    @ApiOperation("最近30天销售额")
    public ResultDTO countSalePrice(@PathVariable Integer status) {
        if(status != null && status.equals(1)){
            status = OrderEnum.SUCCESS.getCode();
        }else if(status.equals(0)){
            status = OrderEnum.FAIL.getCode();
        }
        return ResultUtil.Success(copOrderService.countSalePrice(status,LocalDateTime.now().minusDays(30L)));
    }

    @PostMapping("/ClientBuy/{status}")
    @ApiOperation("最近30天客户消费额")
    public ResultDTO countClientBuy(@PathVariable Integer status) {
        if(status != null && status.equals(1)){
            status = OrderEnum.SUCCESS.getCode();
        }else if(status.equals(0)){
            status = OrderEnum.FAIL.getCode();
        }
        return ResultUtil.Success(copOrderService.countClientBuy(status,LocalDateTime.now().minusDays(30L)));
    }

    @PostMapping("/ClientGen/{status}")
    @ApiOperation("最近30天客户性别")
    public ResultDTO countClientGen(@PathVariable Integer status) {
        if(status != null && status.equals(1)){
            status = OrderEnum.SUCCESS.getCode();
        }else if(status.equals(0)){
            status = OrderEnum.FAIL.getCode();
        }
        return ResultUtil.Success(copOrderService.countClientGen(status,LocalDateTime.now().minusDays(30L)));
    }

}
